# -*- coding: utf-8 -*-
"""
表单行组件
"""
from PySide6.QtWidgets import QWidget, QLineEdit, QTextEdit, QComboBox, QCheckBox
from PySide6.QtCore import Qt

from ..base import TgComponentMixin
from ..inputs import TgLabel, TgLineEdit, TgTextEdit, TgComboBox, TgCheckBox
from ..layouts import TgHBoxLayout


class TgFormRow(QWidget, TgComponentMixin):
    """TG表单行组件 - 标签和输入控件的组合"""
    
    def __init__(self, label_text="", input_widget=None, label_width=None, parent=None):
        super().__init__(parent)
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        self._component_parts = {}
        
        # 设置组件特定属性
        self.label_text = label_text
        self.input_widget = input_widget
        self.label_width = label_width  # 可选的标签宽度
        self.label = None
        
        # 初始化组件
        self.initialize()
    
    def setup_ui(self):
        """初始化UI"""
        layout = TgHBoxLayout(margins='small', spacing='medium')
        
        # 创建标签
        if self.label_text:
            self.label = TgLabel(self.label_text)
            
            # 根据是否指定了标签宽度来设置
            if self.label_width:
                self.label.setFixedWidth(self.label_width)
            else:
                # 设置最小宽度而不是固定宽度，让标签能够适应内容
                self.label.setMinimumWidth(80)
                self.label.setMaximumWidth(150)
            
            # 设置标签右对齐，使其与输入控件更好地对齐
            self.label.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
            layout.addWidget(self.label)
            self.add_component_part('label', self.label)
        
        # 添加输入控件
        if self.input_widget:
            layout.addWidget(self.input_widget, 1)  # 输入控件占用剩余空间
            self.add_component_part('input', self.input_widget)
        
        self.setLayout(layout)
    
    def apply_style(self):
        """应用样式"""
        # 表单行的样式通过各个组件自身的样式来实现
        # 添加一些基本的样式
        self.setStyleSheet(f"""
            QWidget {{
                margin: 2px 0px;
            }}
        """)
        self._style_applied = True
    
    def add_component_part(self, name, component):
        """添加组件部分"""
        self._component_parts[name] = component
    
    def get_component_part(self, name):
        """获取组件部分"""
        return self._component_parts.get(name)
    
    def get_component_parts(self):
        """获取所有组件部分"""
        return self._component_parts.copy()
    
    def get_value(self):
        """获取输入值"""
        if isinstance(self.input_widget, (TgLineEdit, QLineEdit)):
            return self.input_widget.text()
        elif isinstance(self.input_widget, (TgTextEdit, QTextEdit)):
            return self.input_widget.toPlainText()
        elif isinstance(self.input_widget, (TgComboBox, QComboBox)):
            return self.input_widget.currentText()
        elif isinstance(self.input_widget, (TgCheckBox, QCheckBox)):
            return self.input_widget.isChecked()
        return None
    
    def set_value(self, value):
        """设置输入值"""
        if isinstance(self.input_widget, (TgLineEdit, QLineEdit)):
            self.input_widget.setText(str(value))
        elif isinstance(self.input_widget, (TgTextEdit, QTextEdit)):
            self.input_widget.setPlainText(str(value))
        elif isinstance(self.input_widget, (TgComboBox, QComboBox)):
            index = self.input_widget.findText(str(value))
            if index >= 0:
                self.input_widget.setCurrentIndex(index)
        elif isinstance(self.input_widget, (TgCheckBox, QCheckBox)):
            self.input_widget.setChecked(bool(value)) 